package com.codegeek.project.slope.controller;

import com.codegeek.common.utils.SecurityUtils;
import com.codegeek.framework.aspectj.lang.annotation.Log;
import com.codegeek.framework.aspectj.lang.enums.BusinessType;
import com.codegeek.framework.web.controller.BaseController;
import com.codegeek.framework.web.domain.AjaxResult;
import com.codegeek.framework.web.page.TableDataInfo;
import com.codegeek.project.slope.domain.SlopePoint;
import com.codegeek.project.slope.service.ISlopePointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 监测点位Controller
 *
 * @author codegeek
 * @date 2020-02-17
 */
@RestController
@RequestMapping("/slope/point")
@Api(value = "监测点位Controller", tags = {"监测点位操作接口"})
public class SlopePointController extends BaseController {
    @Autowired
    private ISlopePointService slopePointService;

    /**
     * 查询监测点位列表
     */
    @ApiOperation("查询监测点位列表")
    @PreAuthorize("@ss.hasPermi('slope:point:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlopePoint slopePoint) {
        startPage();
        List<SlopePoint> list = slopePointService.selectSlopePointList(slopePoint);
        return getDataTable(list);
    }


    @ApiOperation("没有设置权限的点位列表")
    @GetMapping("/noRoleList")
    public TableDataInfo noRoleList(SlopePoint slopePoint) {
        startPage();
        List<SlopePoint> list = slopePointService.selectSlopePointList(slopePoint);
        return getDataTable(list);
    }

//    /**
//     * 导出监测点位列表
//     */
//    @ApiOperation("导出监测点位列表")
//    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
//    @GetMapping("/export")
//    public AjaxResult export(SlopePoint slopePoint)
//    {
//        List<SlopePoint> list = slopePointService.selectSlopePointList(slopePoint);
//        ExcelUtil<SlopePoint> util = new ExcelUtil<SlopePoint>(SlopePoint.class);
//        return util.exportExcel(list, "point");
//    }

    /**
     * 获取监测点位详细信息
     */
    @ApiOperation("获取监测点位详细信息")
    @GetMapping(value = "/getInfo/{pointId}")
    public AjaxResult getInfo(@PathVariable("pointId") Long pointId) {
        return AjaxResult.success(slopePointService.selectSlopePointById(pointId));
    }

    /**
     * 新增监测点位
     */
    @ApiOperation("新增监测点位")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('slope:point:add')")
    public AjaxResult add(@RequestBody SlopePoint slopePoint) {
        slopePoint.setCreateBy(SecurityUtils.getUsername());
        return toAjax(slopePointService.insertSlopePoint(slopePoint));
    }


    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 删除监测点位
     */
    @ApiOperation("删除监测点位")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove/{pointIds}")
    @PreAuthorize("@ss.hasPermi('slope:point:remove')")
    public AjaxResult remove(Long[] pointIds) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT COUNT(*) from slope_device where point_id = " + pointIds[0]);
        int num = jdbcTemplate.queryForObject(sql.toString(), Integer.class);
        if (num > 0) {
            return AjaxResult.error("此点位绑定了设备，请先取消绑定再删除!");
        } else {
            return toAjax(slopePointService.deleteSlopePointByIds(pointIds));
        }
    }

    @ApiOperation("修改状态是否禁用")
    @PostMapping("/changeStatus")
    @PreAuthorize("@ss.hasPermi('slope:point:disable')")
    public AjaxResult changeStatus(SlopePoint slopePoint) {
        if (slopePoint.getIsValid() == 0) {
            if (jdbcTemplate.queryForObject(" select count(*) from slope_device where point_id='" + slopePoint.getPointId() + "' ", Integer.class) > 0) {
                return AjaxResult.error("点位已绑定设备，请先取消设备绑定才能禁用!");
            }
        }
        return AjaxResult.success(slopePointService.changeStatus(slopePoint));
    }

    /**
     * 修改监测点位
     */
    @ApiOperation("修改监测点位")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('slope:point:edit')")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SlopePoint slopePoint) {
        return toAjax(slopePointService.updateSlopePoint(slopePoint));
    }

    @ApiOperation("检查新增点位名称是否重复")
    @GetMapping("/checkPointName")
    public AjaxResult checkPointName(String name) {
        return AjaxResult.success(slopePointService.checkPointName(name));

    }
}
